#! /bin/sh

# Plait -- Search music library and make one of the supported music
# players play the results.
#
# Copyright (C) 2005-2009 Stephen Jungels
# Copyright (C) 2017-2018 orbea
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.  
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# See COPYING for the full text of the license.
#
# See README for supported platforms and installation instructions.
#
# To contact the author: sjungels@gmail.com


usage ()
{
  echo "Usage is:"
  echo "  \"plait <hint about artist or album or song> [[not] <hint>]...[as <mix>]\", or" 
  echo "Options:"
  echo "  --list,-l         -- show matching tracks but don't play"
  echo "  --mix,-m          -- play tracks matching any hint (make a mix)"
  echo "  --queue,-q        -- add tracks to queue"
  echo "  --random,-r       -- play in random order"
  echo "  --sort            -- play in sorted order"
  echo "  --stripe          -- play in alternating order"
  echo "  --stripe2         -- play in random alternating order"
  echo "  --fade            -- play in gaussian fade order"
  echo "  --group           -- play randomly in groups"
  echo "  --tracks,-t       -- play n tracks, if possible"
  echo "  --mixfile,-f      -- read hints from mixfile"
  echo "  --device,-d       -- choose target device"
  echo "  --platform        -- choose platform"
  echo "  --coverart        -- cover art mode (0 or 1)"
  echo "  --guest,-g        -- use guest configuration"
  echo "  --interactive,-i  -- interactive mode"
  echo "  --cache,-c        -- rebuild cache"
  echo "  --play            "
  echo "  --pause           "
  echo "  --stop            "
  echo "  --next            "
  echo "  --prev            "
  echo "  --help            -- show help"
  echo "  --version         -- show version"
}


version ()
{
  echo "Plait v 1.6.3"
  echo "Written by Stephen Jungels"
  echo ""
  echo "Copyright (c) 2005-2009 by Stephen Jungels.  Released under the GPL."
  echo "Latest version and more info at http://plait.sourceforge.net/"
}


docache ()
{
  echo 1>&2 Caching music directory $MUSICDIR/$SUBDIR

  if test "-$SUBDIR-" = "--"
  then
    rm -f "$HOME/.plait/$CACHE"
    dir="$MUSICDIR"
  else
    touch "$HOME/.plait/$CACHE"
    dir="$MUSICDIR/$SUBDIR"
    cat "$HOME/.plait/$CACHE" | grep -v "$dir" > "$HOME/.plait/cache2"
    mv "$HOME/.plait/cache2" "$HOME/.plait/$CACHE"
  fi
  if test -d "$dir"
  then
    eval "find \"$dir\" -follow -type f" >> "$HOME/.plait/$CACHE"
    sort "$HOME/.plait/$CACHE" | uniq > "$HOME/.plait/$CACHE-tmp"
    mv "$HOME/.plait/$CACHE-tmp" "$HOME/.plait/$CACHE"
  else
    echo 1>&2 The directory $dir was not found 
  fi
  touch "$HOME/.plait/$CACHE"
}


querylibrary ()
{
  rm -f "$HOME/.plait/mixfile"
  OUTMIXFILE=mix0
  QUERY=""
  NOTQUERY=""
  bar=""
  awkhints=""
  colons=""
  while test $# -gt 0
  do
    if test "-$1-" = "-as-" && ! test "-$2-" = "--" && test "-$bar-" = "-|-"
    then
      OUTMIXFILE="$(echo "$2" | sed 's/ /-/g')"
      shift; shift
    elif test "-$1-" = "-not-" && ! test "-$2-" = "--" && test "-$bar-" = "-|-"
    then
      NOTQUERY="$NOTQUERY | grep -Eiv \"$2\""
      echo "not \"$2\"" >> "$HOME/.plait/mixfile"
      shift; shift
    else
      if test $MIX = 1 || test "-$colons-" = "--"
      then
        awkhints="${awkhints}${colons}$1"
        colons="::"
      fi
      if test $MIX = 0 
      then
        QUERY="$QUERY | grep -Ei \"$1\""
      else
        QUERY="${QUERY}${bar}$1"
      fi
      echo "\"$1\"" >> "$HOME/.plait/mixfile"
      shift
    fi
    bar="|"
  done
  if test $MIX = 1 
  then
    QUERY="| grep -Ei \"$QUERY\""
  fi

  STRIPDIR="| sed \"s:$MUSICDIR::\""
  ADDDIR="| sed \"s:^:$MUSICDIR:\""
  eval cat "$HOME/.plait/$CACHE" $STRIPDIR $QUERY $NOTQUERY $ADDDIR > "$HOME/.plait/playlist.m3u"
  if test -e "$HOME/.plait/mixfile"
  then
    sort "$HOME/.plait/mixfile" | uniq > "$HOME/.plait/mixfiles/$OUTMIXFILE"
  fi

  # select device-appropriate tracks

  awk -f $INSTALLDIR/share/plait/device.awk types="$DEVICE" \
    "$HOME/.plait/playlist.m3u" > "$HOME/.plait/playlist0.m3u"

  # optionally process the list interactively

  if test $INTERACTIVE = 1
  then
    awk -f $INSTALLDIR/share/plait/interactive.awk prefix="$MUSICDIR/" levelsep="$LEVELSEP" \
      "$HOME/.plait/playlist0.m3u" > "$HOME/.plait/playlist.m3u"
    mv "$HOME/.plait/playlist.m3u" "$HOME/.plait/playlist0.m3u"
  fi

  # balance and sort

  if test $ORDER = "sort"
  then
    awk -f $INSTALLDIR/share/plait/postprocess.awk hints="$awkhints" order=$ORDER tracks=$TRACKS \
      "$HOME/.plait/playlist0.m3u" state=2 "$HOME/.plait/playlist0.m3u" \
      > "$HOME/.plait/playlist.m3u"
  else 
    awk -f $INSTALLDIR/share/plait/postprocess.awk hints="$awkhints" order=$ORDER tracks=$TRACKS \
      "$HOME/.plait/playlist0.m3u" state=2 "$HOME/.plait/playlist0.m3u" \
      | sort -n | cut -f "2-" > "$HOME/.plait/playlist.m3u"
  fi
}


listmixfiles ()
{
  echo Mixfiles available:
  mkdir -p "$HOME/.plait/mixfiles"
  rm -f "$HOME/.plait/mixfiles/*~"
  ls "$HOME/.plait/mixfiles"
}


prefetch_stream_urls ()
{
  rm -f "$HOME/.plait/playlists.pls"
  cat "$HOME/.plait/playlist.m3u" | grep -i "^http" | awk \
    '{ print "wget >/dev/null 2>&1 -O \"$HOME/.plait/playlist.pls\" \"" \
     $0 "\"; cat \"$HOME/.plait/playlist.pls\" >> \"$HOME/.plait/playlists.pls\""}' \
     > "$HOME/.plait/xme"
  . "$HOME/.plait/xme"

  # make sure there is a trailing slash, for mpg321:
  cat "$HOME/.plait/playlists.pls" | awk 'BEGIN {FS="="} \
    /File1/ {if ($2 ~ /\/$/) print $2; else print $2 "/"}' \
    > "$HOME/.plait/playlist2.m3u"
}


prefetch_or_copy ()
{
  hasurl="$(grep -ci http "$HOME/.plait/playlist.m3u")"
  if test $hasurl -gt 0
  then
    prefetch_stream_urls
  else
    cp "$HOME/.plait/playlist.m3u" "$HOME/.plait/playlist2.m3u"
  fi
}


LINUX="$(uname -s | grep -ic Linux)"
INSTALLDIR="%INSTALLDIR%"

# file types and devices
TYPES=".mp3 .wav .flac .aif .aiff .ogg"
compact="mp3 MP3"
medium="mp3 MP3 ogg OGG wav WAV flac FLAC aif AIF aiff AIFF"
hifi="wav WAV flac FLAC aif AIF aiff AIFF mp3 MP3 ogg OGG"
DEVICE="$hifi"
SETDEVICE=""

# option flags
SHOW=0
QUEUE=0
ORDER="sort"
SETORDER=""
CONFIG=config
CACHE=cache
DOCACHE=0
MIX=0
TRACKS=-4
SETTRACKS=-5
MIXFILES=""
DOMIXFILE=0
DOLISTMIXFILES=0
HINTS=""
HINT1=""
SUBDIR=""
COMMAND=""
SLOW_PATH_TRANSLATION=0
INTERACTIVE=0
LEVELSEP="/"
SETPLATFORM=""
ARTISTIDX=1
ALBUMIDX=2
SONGIDX=3
BASEURL=""
COVERART=0
SETCOVERART=-1


while test $# -gt 0
do
  case "$1" in
    --list|-l)
      SHOW=1
      shift
    ;;

    --queue|-q)
      QUEUE=1
      shift
    ;;

    --random|-r)
      SETORDER="random"
      shift
    ;;

    --stripe)
      SETORDER="stripe"
      shift
    ;;

    --stripe2)
      SETORDER="stripe2"
      shift
    ;;

    --sort)
      SETORDER="sort"
      shift
    ;;

    --fade)
      SETORDER="fade"
      shift
    ;;

    --group)
      SETORDER="group"
      shift
    ;;

    --cache|-c)
      DOCACHE=1
      if ! test "-$2-" = "--"
      then
        SUBDIR="$2"
        shift
      fi
      shift
    ;;

    --coverart)
      if ! test "-$2-" = "--"
      then
        test 2> /dev/null $2 -gt -1
        if test $? -ne 0
        then
          echo 1>&2 Bad value for --coverart, ignoring $1
        else
          SETCOVERART=$2
          shift
        fi
      else
        echo 1>&2 No value for --coverart, ignoring
      fi
      shift
    ;;

    --guest|-g)
      if ! test "-$2-" = "--"
      then
        prefix="$(echo "$2" | sed 's/ /-/g')"
        CONFIG="$prefix-$CONFIG"
        CACHE="$prefix-$CACHE"
        shift
      else
        echo 1>&2 No guest name provided, ignoring $1
      fi
      shift
    ;;

    --device|-d)
      if ! test "-$2-" = "--"
      then
        SETDEVICE="$2"
        shift
      else
        echo 1>&2 No device name provided, ignoring $1
      fi
      shift
    ;;

    --platform)
      if ! test "-$2-" = "--"
      then
        SETPLATFORM="$2"
        shift
      else
        echo 1>&2 No platform provided, ignoring $1
      fi
      shift
    ;;

    --mix|-m)
      MIX=1
      shift
    ;;

    --tracks|-t)
      if ! test "-$2-" = "--"
      then
        if test $2 = "avg"
        then 
          SETTRACKS=-1
        elif test $2 = "min"
        then
          SETTRACKS=-2
        elif test $2 = "max"
        then
          SETTRACKS=-3
        elif test $2 = "all"
        then
          SETTRACKS=-4
        else
          test 2> /dev/null $2 -gt -1
          if test $? -ne 0
          then
            echo 1>&2 Bad track count, ignoring $1
          else
            SETTRACKS=$2
          fi
        fi
        shift
      else
        echo 1>&2 No track count provided, ignoring $1
      fi
      shift
    ;;

    --mixfile|-f)
      if ! test "-$2-" = "--"
      then
        MIXFILES="$MIXFILES $(echo "$2" | sed 's/ /-/g')"
        DOMIXFILE=1
        MIX=1
        shift
      else
        echo 1>&2 No mix file name provided, ignoring $1
      fi
      shift
    ;;

    --play)
      COMMAND=play
      shift
    ;;

    --pause)
      COMMAND=pause
      shift
    ;;

    --stop)
      COMMAND=stop
      shift
    ;;

    --next)
      COMMAND=next
      shift
    ;;

    --prev)
      COMMAND=prev
      shift
    ;;

    --interactive|-i)
      INTERACTIVE=1
      shift
    ;;

    --dump)
      DOLISTMIXFILES=1
      shift
    ;;

    --help)
      usage
      exit 0
    ;;

    --version)
      version
      exit 0
    ;;

    -*)
      echo 1>&2 Ignoring unknown option $1
      shift
    ;;

    *)
      if test "-$HINT1-" = "--"
      then
        HINT1="$1"
      fi
      HINTS="$HINTS \"$1\""
      shift
    ;;

  esac
done

mkdir -p "$HOME/.plait/xspf"
if test -f "$HOME/.plait/$CONFIG"
then
  . "$HOME/.plait/$CONFIG"
else
  echo "INSTALLDIR=$INSTALLDIR" > "$HOME/.plait/$CONFIG"
fi

if test "-$MUSICDIR-" = "--"
then
  echo 1>&2 Please enter the directory where your music is stored.
  while test 1 -gt 0
  do
    read a
    if test -d "$a"
    then 
      MUSICDIR="$a"
      echo >> "$HOME/.plait/$CONFIG" "MUSICDIR=\"$a\""
      echo 1>&2 Configuration complete
      break
    else
      echo 1>&2 Directory $a not found, try again
    fi
  done
fi

if ! test "-$SETPLATFORM-" = "--"
then
  PLATFORM=$SETPLATFORM
fi

if test "-$PLATFORM-" = "--"
then
  xmms="$(which xmms 2>/dev/null)"
  if ! test "-$xmms-" = "--"
  then
    PLATFORM=unix-xmms
  else
    bmp="$(which beep-media-player 2>/dev/null)"
    if ! test "-$bmp-" = "--"
    then
      PLATFORM=unix-beep-media-player
    else
      audacious="$(which audacious 2>/dev/null)"
      if ! test "-$audacious-" = "--"
      then
        PLATFORM=unix-audacious
      else
        amarok="$(which amarok 2>/dev/null)"
        if ! test "-$amarok-" = "--"
        then
          PLATFORM=unix-amarok
        else
          mpc="$(which mpc 2>/dev/null)"
          if ! test "-${mpc}-" = "--"
          then
            PLATFORM=unix-mpc
          else
            mpg="$(which mpg123 2>/dev/null)"
            ogg="$(which ogg123 2>/dev/null)"
            mpg3="$(which mpg321 2>/dev/null)"
            if ! test "-${mpg}${ogg}${mpg3}-" = "--"
            then
              PLATFORM=unix-plaiter
            else
              PLATFORM=any-listonly
              echo 1>&2 No player is selected.  Edit $HOME/.plait/config to select one.
            fi
          fi
        fi
      fi
    fi
  fi
  echo 1>&2 Platform $PLATFORM selected.
  echo >> "$HOME/.plait/$CONFIG" "PLATFORM=$PLATFORM"
fi

export ESPEAKER
MUSICDIR="$(echo "$MUSICDIR" | sed "s:/$::")"

mkdir -p "$HOME/.plait/mixfiles"

if test $SETTRACKS -ge -4
then
  TRACKS=$SETTRACKS
fi

if ! test "-$SETDEVICE-" = "--"
then
  eval DEVICE="\$$SETDEVICE"
fi

if ! test "-$SETORDER-" = "--"
then
  ORDER=$SETORDER
fi

if test $SETCOVERART -ge 0
then
  COVERART=$SETCOVERART
fi

if test $DOCACHE -eq 1
then
  docache
  exit 0
fi

if test $DOLISTMIXFILES -eq 1
then
  listmixfiles
  exit 0
fi


# if there is a command, forward it to the player and quit
if ! test "-$COMMAND-" = "--"
then
  case $PLATFORM in
    unix-mpc)
      mpc $COMMAND > /dev/null
      ;;

    linux-plaiter|unix-plaiter)
      plaiter --$COMMAND
      ;;

    linux-xmms|unix-xmms|unix_xmms)
      case $COMMAND in
        play)
          xmms-shell -e play > /dev/null 2>&1
          ;;

        pause)
          xmms-shell -e pause > /dev/null 2>&1
          ;;

        stop)
          xmms-shell -e stop > /dev/null 2>&1
          ;;

        next)
          xmms-shell -e next > /dev/null 2>&1
          ;;

        prev)
          xmms-shell -e previous > /dev/null 2>&1
          ;;

      esac
      if ! test $? = 0
      then
        echo 1>&2 Platform $PLATFORM requires the package xmms-shell to send command $COMMAND
      fi
      ;;

    linux-beep-media-player|unix-beep-media-player)
      case $COMMAND in
        play)
          beep-media-player --play > /dev/null 2>&1
          ;;

        pause)
          beep-media-player --play-pause > /dev/null 2>&1
          ;;

        stop)
          beep-media-player --stop > /dev/null 2>&1
          ;;

        next)
          beep-media-player --fwd > /dev/null 2>&1
          ;;

        prev)
          beep-media-player --rew > /dev/null 2>&1
          ;;

      esac
      ;;

    linux-audacious|unix-audacious)
      case $COMMAND in
        play)
          audacious --play > /dev/null 2>&1
          ;;

        pause)
          audacious --play-pause > /dev/null 2>&1
          ;;

        stop)
          audacious --stop > /dev/null 2>&1
          ;;

        next)
          audacious --fwd > /dev/null 2>&1
          ;;

        prev)
          audacious --rew > /dev/null 2>&1
          ;;

      esac
      ;;

    linux-amarok|unix-amarok)
      case $COMMAND in
        play)
          amarok --play > /dev/null 2>&1
          ;;

        pause)
          amarok --pause > /dev/null 2>&1
          ;;

        stop)
          amarok --stop > /dev/null 2>&1
          ;;

        next)
          amarok --next > /dev/null 2>&1
          ;;

        prev)
          amarok --previous > /dev/null 2>&1
          ;;

      esac
      ;;

    linux-mplayer|unix-mplayer)
      case $COMMAND in
        stop)
          killall mplayer > /dev/null 2>&1
          ;;
      esac
      ;;

    *)
      echo 1>&2 Platform $PLATFORM does not support command $COMMAND
      ;;

  esac
  exit 0
fi

if ! test "-$HINTS-" = "--" || test $DOMIXFILE = 1
then

  if ! test -f "$HOME/.plait/$CACHE"
  then
    docache
  fi

  if test $DOMIXFILE = 1
  then
    LISTNAME="$(echo \"$MIXFILES\" | sed \"s/^ //\") tracks"
  else
    LISTNAME="$HINT1 tracks"
  fi
  echo "LISTNAME=\"$LISTNAME\"" > "$HOME/.plait/listname"

  if test $DOMIXFILE -eq 0
  then
    eval querylibrary $HINTS
  else
    filehints=""
    for MIXFILE in $MIXFILES
    do
      if test -e "$HOME/.plait/mixfiles/$MIXFILE"
      then
        filehints="$filehints $(cat "$HOME/.plait/mixfiles/$MIXFILE" | awk '{printf "%s ", $0}')"
      else
        echo 1>&2 Mixfile $MIXFILE not found
      fi
    done
    if test "-$HINTS-" = "--" && test "-$filehints-" = "--"
    then
      echo 1>&2 No hints, exiting
      exit 0
    else
      eval querylibrary $filehints $HINTS
    fi
  fi
else
  # make sure there's at least an empty playlist
  touch "$HOME/.plait/playlist.m3u"
fi

if test "-$LISTNAME-" = "--"
then 
  if test -f "$HOME/.plait/listname"
  then 
    . "$HOME/.plait/listname"
  fi
  if test "-$LISTNAME-" = "--"
  then 
    LISTNAME="plait tracks"
    echo "LISTNAME=\"$LISTNAME\"" > "$HOME/.plait/listname"
  fi
fi

if test $SHOW = 1
then
  cat "$HOME/.plait/playlist.m3u"
else
  cuts="$(cat "$HOME/.plait/playlist.m3u" | wc -l)"
  if test $cuts = 0
  then
    echo 1>&2 Playlist empty, try --list or --help
  else
    if ! test -d "$MUSICDIR"
    then
      echo 1>&2 The music directory is unavailable, exiting
      exit 1
    fi
    case $PLATFORM in

      linux-xmms|unix-xmms|unix_xmms)

        prefetch_or_copy
        if test $QUEUE -eq 0
        then
          xmms "$HOME/.plait/playlist2.m3u" &
        else
          xmms --enqueue "$HOME/.plait/playlist2.m3u" &
        fi

        ;;

      linux-mpg321|unix-mpg321)

        prefetch_or_copy
        killall > /dev/null 2>&1 mpg321
        mpg321 -q --list "$HOME/.plait/playlist2.m3u" &

        ;;

      linux-mpg321-esd|unix-mpg321-esd)

        prefetch_or_copy
        killall > /dev/null 2>&1 mpg321
        if ! test "-$ESPEAKER-" = "--"
        then
          SERVEROPT="-a $ESPEAKER"
        fi
        mpg321 -q -o esd $SERVEROPT --list "$HOME/.plait/playlist2.m3u" &

        ;;

      linux-mpg123|unix-mpg123)

        prefetch_or_copy
        killall > /dev/null 2>&1 mpg123
        mpg123 -q --list "$HOME/.plait/playlist2.m3u" &

        ;;

      linux-ogg123|unix-ogg123)

        prefetch_or_copy
        killall > /dev/null 2>&1 ogg123
        ogg123 -q --list "$HOME/.plait/playlist2.m3u" &

        ;;

      linux-ogg123-esd|unix-ogg123-esd)

        prefetch_or_copy
        killall > /dev/null 2>&1 ogg123
        ogg123 -d esd -q --list "$HOME/.plait/playlist2.m3u" &

        ;;

      linux-mpg123-esd|unix-mpg123-esd)

        prefetch_or_copy
        killall > /dev/null 2>&1 mpg123-esd
        mpg123-esd -q --list "$HOME/.plait/playlist2.m3u" &

        ;;

      unix-mpc)

        prefetch_or_copy
        if test $QUEUE = 0
        then
          mpc clear > /dev/null
        fi

        MPDLIBRARY="$MUSICDIR"
        while read file
        do
          # chop off absolute path
          file="$(echo $file | sed -e "s#$MPDLIBRARY/\(.*\)#\1#")"
          mpc add "${file}" > /dev/null
        done < "$HOME/.plait/playlist2.m3u"
        if test $QUEUE = 0
        then
          mpc play
        fi

        ;;

      linux-plaiter|unix-plaiter)

        prefetch_or_copy
        if test $QUEUE = 0
        then
          plaiter "$HOME/.plait/playlist2.m3u" &
        else
          plaiter --queue "$HOME/.plait/playlist2.m3u" &
        fi

        ;;

      linux-beep-media-player|unix-beep-media-player)

        prefetch_or_copy
        if test $QUEUE = 0
        then
          beep-media-player "$HOME/.plait/playlist2.m3u" &
        else
          beep-media-player --enqueue "$HOME/.plait/playlist2.m3u" &
        fi

        ;;

      linux-audacious|unix-audacious)

        prefetch_or_copy
        if test $QUEUE = 0
        then
	  # audacious will print stuff to the console
          audacious "$HOME/.plait/playlist2.m3u" >/dev/null 2>&1 &
        else
          audacious --enqueue "$HOME/.plait/playlist2.m3u" >/dev/null 2>&1 &
        fi

        ;;

      linux-amarok|unix-amarok)

        prefetch_or_copy
        if test $QUEUE = 0
        then
          amarok "$HOME/.plait/playlist2.m3u" &
        else
          amarok --enqueue "$HOME/.plait/playlist2.m3u" &
        fi

        ;;

      any-listonly)

        cat "$HOME/.plait/playlist.m3u"

        ;;

      any-xspf)

        prefetch_or_copy
        awk -f $INSTALLDIR/share/plait/xspf.awk -v title="$LISTNAME" art=$COVERART \
          plait="$HOME/.plait" coverprog="$INSTALLDIR/share/plait/coverart.awk" \
          sep="$LEVELSEP" d="$MUSICDIR" ar=$ARTISTIDX al=$ALBUMIDX so=$SONGIDX url="$BASEURL" \
          "$HOME/.plait/playlist2.m3u" > "$HOME/.plait/xspf/playlist.xspf"
        sed -e "s:%TITLE%:$LISTNAME:" $INSTALLDIR/share/plait/playlist.html \
          > "$HOME/.plait/xspf/index.html" 

        echo 1>&2 "Playlist directory $HOME/.plait/xspf ready for upload"

        ;;

      any-xspf-copy)

        prefetch_or_copy
        awk -f $INSTALLDIR/share/plait/xspf.awk -v title="$LISTNAME" art=$COVERART \
          plait="$HOME/.plait" coverprog="$INSTALLDIR/share/plait/coverart.awk" \
          sep="$LEVELSEP" d="$MUSICDIR" ar=$ARTISTIDX al=$ALBUMIDX so=$SONGIDX url="$BASEURL" \
          "$HOME/.plait/playlist2.m3u" > "$HOME/.plait/xspf/playlist.xspf"
        sed -e "s:%TITLE%:$LISTNAME:" $INSTALLDIR/share/plait/playlist.html \
          > "$HOME/.plait/xspf/playlist.html" 

        if ! test $hasurl -gt 0
        then
          echo 1>&2 "Copying mp3 files to $HOME/.plait/xspf"
          awk -f $INSTALLDIR/share/plait/treecopy.awk d="$MUSICDIR/" to="$HOME/.plait/xspf/" \
            "$HOME/.plait/playlist2.m3u" | sh
        fi
        echo 1>&2 "Playlist directory $HOME/.plait/xspf ready for upload"

        ;;

      linux-mplayer|unix-mplayer)

        prefetch_or_copy
        killall > /dev/null 2>&1 mplayer
        mplayer -really-quiet -playlist "$HOME/.plait/playlist2.m3u" &

        ;;

      any-s3)

        ln2="$(echo "$LISTNAME" | sed "s:/:-:g" | sed 's: :-:g')"
        STRIPDIR=" | sed \"s:$MUSICDIR::\""
        ADDURL=" | sed \"s~^~$BASEURL~\""
        eval cat "$HOME/.plait/playlist.m3u" $STRIPDIR $ADDURL > "$HOME/.plait/playlist2.m3u"
        cp "$HOME/.plait/playlist2.m3u" $MUSICDIR/$ln2.m3u
        echo Playlist URL: $BASEURL/$ln2.m3u

        ;;

    esac
  fi
fi
